package com.agentfactory.teleoreactive.compiler;

public class PredicateConstructorVisitor implements TeleoReactiveParserVisitor {

	@Override
	public Object visit(SimpleNode node, Object data) {
		String firstOrder = "";
		if (node.toString().equals("Predicate")) {
			firstOrder += (String) node.jjtGetValue();// add the name
			if(node.jjtGetNumChildren() > 0){ // if it has children add bracket for terms
				firstOrder += "(";
			}
			for(int i = 0; i < node.jjtGetNumChildren(); i++){
				firstOrder += (String) visit((SimpleNode)node.jjtGetChild(i), data); // add terms
				if(i < node.jjtGetNumChildren()-1){ 
					firstOrder += ",";
				}
			}
			if(node.jjtGetNumChildren() > 0) firstOrder +=")";
		} else if (node.toString().equals("Term")) {
			String term = (String) node.jjtGetValue();
			if (term != null) {
				if (term.startsWith("?")) {
					firstOrder += term;
				} else if (term.equals("+")|term.equals("-")|term.equals("*")|term.equals("/")) {
					firstOrder += visit((SimpleNode) node.jjtGetChild(0), null);
					firstOrder += term;
					firstOrder += visit((SimpleNode) node.jjtGetChild(1), null);
				}
			} else {
				firstOrder += visit((SimpleNode) node.jjtGetChild(0), null);
			}
		}  else if (node.toString().equals("TermTypes")) {
			firstOrder += visit((SimpleNode) node.jjtGetChild(0), null);
		} else if (node.toString().equals("Variable")) {
			firstOrder += (String) node.jjtGetValue();
		} else if (node.toString().equals("String")) {
			firstOrder += (String) node.jjtGetValue();
		} else if (node.toString().equals("Function")) {
			firstOrder += (String) node.jjtGetValue();

			if(0 < node.jjtGetNumChildren())
			firstOrder +=  "(";
			for (int i=0; i<node.jjtGetNumChildren(); i++) {
				firstOrder += visit((SimpleNode) node.jjtGetChild(i), data);
				if(i < node.jjtGetNumChildren()-1) firstOrder += ",";
			}
			if(0 < node.jjtGetNumChildren()) firstOrder += ")";
		} else if (node.toString().equals("List")) {
		if(0 < node.jjtGetNumChildren())
		firstOrder +=  "[";
		for (int i=0; i<node.jjtGetNumChildren(); i++) {
			firstOrder += visit((SimpleNode) node.jjtGetChild(i), data);
			if(i < node.jjtGetNumChildren()-1) firstOrder += ",";
		}
		if(0 < node.jjtGetNumChildren()) firstOrder += "]";
	}
		return firstOrder;
	}
}
